/* eslint-disable */
import { QUESTION_SIZE_MAP } from "@/assets/js/constant";
/**
 * 工具函数
 */
// 由随机数+时间戳生成一个简单的唯一标识
export function uuid() {
  return `${Math.random()
    .toString(16)
    .slice(2)}-${new Date().getTime()}`;
}

// 生成html空格
export function createHtmlSpace(num) {
  return `${[...new Array(num)]
    .map(() => "&nbsp;")
    .join("")
    .replace(/\s/, "")}`;
}

// 简单判断数据对象是否相等
export function isObjectEqual(obj1, obj2) {
  return JSON.stringify(obj1) === JSON.stringify(obj2);
}

// 简单的数据克隆
export function clone(obj) {
  let rt;
  try {
    rt = JSON.parse(JSON.stringify(obj));
  } catch (err) {
    rt = null;
  }
  return rt;
}

// 下载文件
export function downloadFile(fileLink) {
  const linkEle = document.createElement("a");
  linkEle.setAttribute("href", fileLink);
  document.body.appendChild(linkEle);
  linkEle.click();
  document.body.removeChild(linkEle);
}

export function getCoordFromIndex({
  index = 0, // 索引
  colCount = 3, // 列数
  rowCount = 5, // 行数
  orderType = "col", // 列主序还是行主序
  itemWidth = 0, // 单个元素的宽度
  itemHeight = 0, // 单个元素的高度
  splitHeight = 0, // 元素排完(行数 * 列数)后，需要空出一定的距离再排下面的元素
  paddingTopHeight = 0
}) {
  const coord = {
    x: 0,
    y: 0
  };
  if (orderType === "col") {
    const xIndex = Math.floor(index / rowCount);
    const yIndex = index % rowCount;
    const offsetIndex = Math.floor(index / (colCount * rowCount));
    const offsetY = offsetIndex * rowCount;
    coord.x = (xIndex % colCount) * itemWidth;
    coord.y =
      yIndex * itemHeight + offsetY * itemHeight + offsetIndex * splitHeight;
  } else if (orderType === "row") {
    const xIndex = index % colCount;
    const yIndex = Math.floor(index / colCount);
    const offsetIndex = Math.floor(index / (colCount * rowCount));
    coord.x = xIndex * itemWidth;
    coord.y = yIndex * itemHeight + offsetIndex * splitHeight;
  }
  coord.y += paddingTopHeight;
  return coord;
}

export function getItemPosition(
  item,
  index,
  offset = { offsetLeft: 0, offsetTop: 0 }
) {
  const { itemWidth, itemHeight, rowCount } = item;
  const x = Math.floor(index / rowCount) * itemWidth + offset.offsetLeft;
  const y = (index % rowCount) * itemHeight + offset.offsetTop;
  return { x, y };
}

export function getSelectData({ contents, rowCount }, colWidth) {
  const {
    itemHeight,
    paddingTopHeight,
    splitHeight
  } = QUESTION_SIZE_MAP.select;
  const rt = [];
  let offsetLeft = 0;
  let offsetTop = paddingTopHeight;
  // 按列分割
  contents.forEach((item, contentIndex) => {
    let arr = [];
    const { quesType, startId, endId, itemWidth, optionCount } = item;
    for (let id = startId; id <= endId; id += 1) {
      arr.push({
        id,
        quesType,
        splitHeight,
        itemWidth,
        itemHeight,
        rowCount,
        optionCount
      });
      // 到了每一列的结尾
      if (id === endId || (id - startId) % rowCount === rowCount - 1) {
        // console.log(id, endId, itemWidth);
        arr = arr.map((arrItem, index) => ({
          ...arrItem,
          position: getItemPosition(arrItem, index, { offsetLeft, offsetTop })
        }));
        offsetLeft += itemWidth;
        // 计算下一列的宽度
        let nextItemWidth = itemWidth;
        if (id === endId) {
          if (contents[contentIndex + 1]) {
            nextItemWidth = contents[contentIndex + 1].itemWidth;
          } else {
            nextItemWidth = 0;
          }
        }
        // 当前行没有剩余空间了
        if (offsetLeft + nextItemWidth > colWidth) {
          offsetLeft = 0;
          // offsetTop += rowCount * itemHeight + splitHeight + paddingTopHeight;
          offsetTop += rowCount * itemHeight + splitHeight;
        }
        rt.push(arr);
        arr = [];
      }
    }
  });
  return rt;
}

export function getSelectHeight(contents, rowCount, noSplitHeight, maxWidth) {
  const { titleHeight } = QUESTION_SIZE_MAP;
  const { itemHeight, paddingTopHeight } = QUESTION_SIZE_MAP.select;
  let h = Math.max(
    ...getSelectData({ contents, rowCount }, maxWidth).map(itemList =>
      Math.max(...itemList.map(x => x.position.y))
    )
  ).toFixed(3);
  h = (h * 1e3 + itemHeight * 1e3 + paddingTopHeight * 1e3) / 1e3;
  h = Math.ceil(h / noSplitHeight) * noSplitHeight;
  return h + titleHeight;
}
